package com.yjlove.myplay.ui.activity;

import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Matrix;
import android.graphics.drawable.BitmapDrawable;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;

import com.google.zxing.BarcodeFormat;
import com.google.zxing.BinaryBitmap;
import com.google.zxing.ChecksumException;
import com.google.zxing.DecodeHintType;
import com.google.zxing.FormatException;
import com.google.zxing.MultiFormatWriter;
import com.google.zxing.NotFoundException;
import com.google.zxing.RGBLuminanceSource;
import com.google.zxing.Result;
import com.google.zxing.WriterException;
import com.google.zxing.common.BitMatrix;
import com.google.zxing.common.HybridBinarizer;
import com.google.zxing.qrcode.QRCodeReader;
import com.yjlove.myplay.R;
import com.yjlove.myplay.base.BaseActivity;

import java.util.Hashtable;

/**
 * 作者 YJlvoe
 * 创建时间 2017/1/23.
 */
public class ZXingTestActivity extends BaseActivity implements View.OnClickListener {

    private Button mCreate;
    private Button mScan;
    private ImageView mImage;
    private TextView mResult;

    @Override
    protected String getActivityTitle() {
        return getIntent().getStringExtra("title");
    }

    @Override
    protected int getActivityView() {
        return R.layout.activity_zxingtest;
    }

    @Override
    protected void initView() {
        mCreate = (Button) findViewById(R.id.zxing_create);
        mScan = (Button) findViewById(R.id.zxing_scan);
        mImage = (ImageView) findViewById(R.id.zxing_image);
        mResult = (TextView) findViewById(R.id.zxing_result);
    }

    @Override
    protected void initData() {
        mCreate.setOnClickListener(this);
        mScan.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.zxing_create:
                try {
                    create2Code();
                } catch (WriterException e) {
                    e.printStackTrace();
                }
                break;
            case R.id.zxing_scan:
                break;
        }
    }

    //生成二维码
    public void create2Code() throws WriterException {
        String content = "http://yjlove.top/";
        Bitmap bmp = createTwoCode(content,300);
        Bitmap logoBmp = small(BitmapFactory.decodeResource(getResources(), R.mipmap.icon));
        Bitmap bitmap = Bitmap.createBitmap(bmp.getWidth(), bmp.getHeight(), bmp.getConfig());
        Canvas canvas = new Canvas(bitmap);
        canvas.drawBitmap(bmp, 0, 0, null);
        canvas.drawBitmap(logoBmp, bmp.getWidth() / 2 - logoBmp.getWidth() / 2, bmp.getHeight() / 2 - logoBmp.getHeight() / 2, null);
        mImage.setImageBitmap(bitmap);
    }

    /**
     * 方法说明：生成无图片二维码
     */
    @SuppressWarnings("unused")
    private Bitmap createTwoCode(String content) throws WriterException {
        BitMatrix matrix = new MultiFormatWriter().encode(content, BarcodeFormat.QR_CODE, 300, 300);
        int width = matrix.getWidth();
        int height = matrix.getHeight();
        int[] pixels = new int[width * height];
        for (int y = 0; y < height; y++) {
            for (int x = 0; x < width; x++) {
                if (matrix.get(x, y)) {
                    pixels[y * width + x] = 0xff000000;
                }
            }
        }
        Bitmap bitmap = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
        bitmap.setPixels(pixels, 0, width, 0, 0, width, height);
        return bitmap;
    }

    /**
     * 方法说明：生成有图片二维码
     */
    @SuppressWarnings("unused")
    private Bitmap createTwoCode(String str, int widthAndHeight) throws WriterException {
        Hashtable<DecodeHintType, String> hints = new Hashtable<DecodeHintType, String>();
        hints.put(DecodeHintType.CHARACTER_SET, "utf-8");
        BitMatrix matrix = new MultiFormatWriter().encode(str, BarcodeFormat.QR_CODE, widthAndHeight, widthAndHeight);
        int width = matrix.getWidth();
        int height = matrix.getHeight();
        int[] pixels = new int[width * height];
        for (int y = 0; y < height; y++) {
            for (int x = 0; x < width; x++) {
                if (matrix.get(x, y)) {
                    pixels[y * width + x] = 0xff000000;
                }
            }
        }
        Bitmap bitmap = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
        bitmap.setPixels(pixels, 0, width, 0, 0, width, height);
        return bitmap;
    }

    /**
     * 方法说明：缩小Bitmap
     */
    @SuppressWarnings("unused")
    private static Bitmap small(Bitmap bitmap) {
        Matrix matrix = new Matrix();
        matrix.postScale(0.2f, 0.2f);
        Bitmap resizeBmp = Bitmap.createBitmap(bitmap, 0, 0, bitmap.getWidth(),
                bitmap.getHeight(), matrix, true);
        return resizeBmp;
    }

    // 解析QR图片
//    private void scanningImage() {
//
//        Hashtable<DecodeHintType, String> hints = new Hashtable<DecodeHintType, String>();
//        hints.put(DecodeHintType.CHARACTER_SET, "utf-8");
//
//        // 获得待解析的图片
//        Bitmap bitmap = ((BitmapDrawable) mImage.getDrawable()).getBitmap();
//        RGBLuminanceSource source = new RGBLuminanceSource(bitmap);
//        BinaryBitmap bitmap1 = new BinaryBitmap(new HybridBinarizer(source));
//        QRCodeReader reader = new QRCodeReader();
//        Result result;
//        try {
//            result = reader.decode(bitmap1);
//            result=reader.decode(bitmap1, hints );
//            // 得到解析后的文字
//            mResult.setText(result.getText());
//        } catch (NotFoundException e) {
//            e.printStackTrace();
//        } catch (ChecksumException e) {
//            e.printStackTrace();
//        } catch (FormatException e) {
//            e.printStackTrace();
//        }
//    }
}
